﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
using System.Text;

public class UIPanel_GameOver : UIPanelBase {
    Transform panelRoot;
    Transform btnBack;
    Transform panelOverData;

    public override void OnInit(Transform root)
    {
        base.OnInit(root);
        panelRoot = root;
        InitGameObj();
        InitEvent();
    }

    void InitGameObj()
    {
        btnBack = panelRoot.Find("Button_back_to_start");
        panelOverData = panelRoot.Find("Panel_over_data");
    }
    void InitEvent()
    {
        Button btn_back = btnBack.GetComponent<Button>();
        btn_back.onClick.RemoveAllListeners();
        btn_back.onClick.AddListener(Back);
    }


    void Back()
    {
        UIManager.ShowUI<UIPanel_Start>(()=> {
            UIManager.CloseUI<UIPanel_Game>();
            UIManager.CloseUI<UIPanel_GameOver>();
        });
    }

    public void OnGameOver(GameMode gm,string picHash,int step,int time)
    {
        //save data
        string configLevelData = "level_config.cfg";
        string configPath = Path.Combine(FileUtil.configFolder,configLevelData);
        MyJson.JsonNode_Object jObj = null;

        int bestStep = 0;
        int bestTime = 0;

        if (File.Exists(configPath))
        {
            byte[] bs = File.ReadAllBytes(configPath);
            string str = UTF8Encoding.UTF8.GetString(bs);
            if (!string.IsNullOrEmpty(str))
            {
                Debug.LogError("str:"+ str);
                jObj = MyJson.Parse(str) as MyJson.JsonNode_Object;
            }
            else
            {
                jObj = new MyJson.JsonNode_Object();
            }
        }
        else
        {
            jObj = new MyJson.JsonNode_Object();
        }
        //hash {mid{ step:time},easy{},hard{}}
        bool isStepBest = false;
        bool isTimeBest = false;

        bestStep = step;
        bestTime = time;

        string curMode = gm.ToString();
        if (jObj.ContainsKey(picHash))
        {
            MyJson.JsonNode_Object jObjCurLevel = jObj[picHash] as MyJson.JsonNode_Object;
            if (jObjCurLevel.ContainsKey(curMode))
            {
                MyJson.JsonNode_Object jObjCurMod = jObjCurLevel[curMode] as MyJson.JsonNode_Object;
                int lastBestStep = jObjCurMod["step"].AsInt();
                int lastBestTime = jObjCurMod["time"].AsInt();
                if (step <= lastBestStep)
                {
                    isStepBest = true;
                    jObjCurMod["step"] = new MyJson.JsonNode_ValueNumber(step);
                }
                else
                {
                    bestStep = lastBestStep;
                }

                if (time <= lastBestTime)
                {
                    isTimeBest = true;
                    jObjCurMod["time"] = new MyJson.JsonNode_ValueNumber(time);
                }
                else
                {
                    bestTime = lastBestTime;
                }
                jObjCurLevel[curMode] = jObjCurMod;
            }
            else
            {
                isStepBest = true;
                isTimeBest = true;
                MyJson.JsonNode_Object jObjCurMod = new MyJson.JsonNode_Object();
                jObjCurMod["step"] = new MyJson.JsonNode_ValueNumber(step);
                jObjCurMod["time"] = new MyJson.JsonNode_ValueNumber(time);
                jObjCurLevel[curMode] = jObjCurMod;
            }
            jObj[picHash] = jObjCurLevel;
        }
        else
        {
            isStepBest = true;
            isTimeBest = true;
            MyJson.JsonNode_Object jObjCurLevel = new MyJson.JsonNode_Object();
            MyJson.JsonNode_Object jObjCurMode = new MyJson.JsonNode_Object();
            jObjCurMode["step"] = new MyJson.JsonNode_ValueNumber(step);
            jObjCurMode["time"] = new MyJson.JsonNode_ValueNumber(time);
            jObjCurLevel[curMode] = jObjCurMode;
            jObj[picHash] = jObjCurLevel;
        }
        FileUtil.SaveFile(FileUtil.configFolder, configLevelData, jObj.ToString());
        InitOverUI(isStepBest, isTimeBest, step, time,  bestStep, bestTime);
    }

    void InitOverUI(bool isStepBest,bool isTimeBest,int step,int time,int bestStep,int bestTime)
    {
        panelOverData.Find("txt_use_step/tex_cur").GetComponent<CustomNumberText>().text = step + "";
        panelOverData.Find("txt_use_time/tex_cur").GetComponent<CustomNumberText>().text = time + "";
        panelOverData.Find("img_best_step/best_step").GetComponent<CustomNumberText>().text = ""+bestStep ;
        panelOverData.Find("img_best_time/best_time").GetComponent<CustomNumberText>().text = ""+bestTime;
        panelOverData.Find("txt_use_step/img_new").gameObject.SetActive(isStepBest);
        panelOverData.Find("txt_use_time/img_new").gameObject.SetActive(isTimeBest);
    }

    public override void Update()
    {
        base.Update();
    }
    public override void OnClose()
    {
        base.OnClose();
        CommandController.instance.UnRegCommandEvent();
    }
}
